<!-- 
说明：品牌表表单
作者：nwl
日期：2018-10-12 10:5:48
版本：1.0.0
-->
<template>
  <el-dialog :title="title"
             append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="600px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="loadingText"
             v-loading="busying">
      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="品牌名称"
                        prop="bdname"
                        :mk-le-length="100"
                        required>
            <el-input v-model="form.bdname"></el-input>
          </mk-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './form-data'
import cloneDeep from 'lodash.clonedeep'
export default {
  name: 'z-series-brand-form',
  mixins: [MkBase],
  props: {
    psid: String,
    isAdd: Boolean,
    title: String,
    btnName: String,
    row: Object
  },
  mounted () {
    if (this.isAdd === false) {
      this.form = cloneDeep(this.row)
    }
  },
  data () {
    const form = formData()
    return {
      ui: {
        formLabelWidth: '100px'
      },
      form,
      busying: false,
      loadingText: '正在保存'
    }
  },
  methods: {
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.busying = true
        try {
          let data = await this[this.isAdd === false ? '$put' : '$post'](
            `/zSeriesBrand/${
              this.isAdd === false ? 'update' : 'add'
            }.json`,
            {
              zSeriesBrand: this.form
            }
          )
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.busying = false
        }
      }
    },
    handleClose () {
      this.$emit('close')
    }
  }
}
</script>
